package com.njit.mailm.core.controller;

import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.core.bean.Email;
import com.njit.mailm.core.bean.MessageWrap;
import com.njit.mailm.core.bean.Page;
import com.njit.mailm.core.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/4/9.
 */
@RequestMapping(value = "/manager")
@Controller
public class ManagerController{
    @Autowired
    private ManagerService managerService;

    /**
     * 将邮件放入回收箱
     * @param ids
     */
    @ResponseBody
    @RequestMapping(value = "/deleteToTrash",method = RequestMethod.POST)
    public void deleteMessageItems(@RequestParam("ids[]") String[] ids){

        managerService.delete(ids);
    }

    /**
     * 设置已读（未读）
     * @param ids 邮箱id数组
     * @param isread 目前状态，true为目前已读
     */
    @ResponseBody
    @RequestMapping(value = "/setRead",method = RequestMethod.POST)
    public void setRead(@RequestParam("ids[]") String[] ids,@RequestParam("isread") boolean isread){
        managerService.setRead(ids,isread);
    }

    @RequestMapping(value="/trash",method = RequestMethod.GET)
    public String showTrash(HttpSession session,Integer page, Model model){

        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        Page p = new Page();
        p.setPage(page);
        p.setTotalItems(managerService.getDeleteNumber(email));
        model.addAttribute("currentPage", p);
        List<MessageWrap> messages = managerService.listMessageFromTrash(email.getId());
        model.addAttribute("messages" ,  messages);
        return "trash";
    }

    @ResponseBody
    @RequestMapping(value = "showUnreadNumber",method = RequestMethod.POST)
    public int showMailTotalMessageNumber(String emailAccount){

        return managerService.getTotalMessageNumber(emailAccount);
    }

    @ResponseBody
    @RequestMapping(value = "setStar",method = RequestMethod.POST)
    public void setStar(boolean setStar,String id){
        managerService.setStar(setStar,id);
    }

    @ResponseBody
    @RequestMapping(value = "getStarNumber",method = RequestMethod.GET)
    public int getStarNumber(HttpSession session){
        Email email = (Email) session.getAttribute(MailConstant.CURRENTEMAIL);
        return managerService.getStarTotal(email);
    }

}
